Ext.require(  
        [  
            'Ext/ux/XHtmlEditor'  
         ]          
);

guideTheme = function (){
	
    Ext.define('themeModel',{
        extend: 'Ext.data.Model',
        fields: [
            {name: 'themeId',type: 'String'},
             {name: 'themeName',type: 'String'},
             {name: 'contentText', type: 'String'},
             {name: 'createDate', type: 'String'},
             {name: 'status',type: 'String'},
             {name:'remark',type:'String'}
         ],
    idProperty : 'themeId'
    });
	
//    var isVisibleStore = getStaticDataStore('isVisible');
    
	var themeStore = Ext.create('Ext.data.Store',{
		pageSize:8,
		model: themeModel,
	    autoLoad:true,
	    proxy:{
	        type: 'ajax',
	        url: 'queryThemeListAction.action',
	        reader:{
	           type: 'json',
	           root: 'resultsList.list',            
	           totalProperty: 'resultsList.total'
	        },
	        extraParam:{
	            	
	        }
	   }
	       
	   });
	var isWork = getStaticDataStore("status");
	//var checkBox = Ext.create('Ext.selection.CheckboxModel');
	 
 
   // var sm=new Ext.grid.CheckboxSelectionModel();
	var grid = Ext.create('Ext.grid.GridPanel',{
		store:themeStore,
		columnLines:true,
		//selModel:checkBox,
		disableSelection:false,
		loadMask:true,
	
		columns:[
		         {xtype: 'rownumberer',text:'序号',width:40},
		         {text:'编号',width:'5%',dataIndex:'themeId',sortable:true},
		         {text:'主题名称',width:'10%',dataIndex:'themeName',sortable:true},
		       /*  {text:'内容',width:'40%',dataIndex:'contentText'},*/
		         {text:"创建时间",width:'15%',dataIndex:'createDate',renderer : function(_registerDate){
		             return _registerDate.replace("T"," ").substring(0,16);
		         }},
		         {text:'备注',width:'15%',dataIndex:'remark'},
		         {text:'状态',width:'15%',dataIndex:'status',renderer:function(v){
        			 var index = isWork.find('valueId',v); 
        			 if(index != -1){
        				 var r = isWork.getAt(index);
        				 return r.get("valueName");
        			 }
        			 else{
        				 return v;
        			 }
        		 }}
		         
		         ],
		
		width:'100%',
		title:"指导主题列表",
		loadMask:true,
		pageSize: 5,
		dockedItems:[{
			dock:'top',
			xtype:'toolbar',//"<span style='color:red'>双击可以修改</span>",'-',	
			items:[{
				width:300,
				fieldLabel:'搜索',
				labelWidth:70,
				xtype:'searchfield',
				store:themeStore			
			},{
				xtype:'label',
				html:'<div style="font-size:12px;color:red">主题名称模糊查询</div>'
			},{
				text:'修改',
				iconCls:'Applicationedit',
				xtype:'button',
				handler:function(){
					var selectedKeys = grid.getSelectionModel().getSelection();
					if(selectedKeys.length == 0){
						Ext.Msg.alert("提示", "请选中一条记录！");
						return;
					}
					newWindow(false,selectedKeys[0]);
				}
			},'->',{
				text:'添加',
				iconCls:'Add',
				xtype:'button',
				handler:function(){
					newWindow(true,null);
				}
			},'-',{
				text:'删除',
				iconCls:'Decline',
				xtype:'button',
				handler:function(){
					var selectedKeys = grid.getSelectionModel().getSelection();
					if(selectedKeys.length == 0){
						Ext.Msg.alert("提示", "请选中一条记录！");
						return;
					}
					Ext.MessageBox.confirm('提示', '确定删除编号为'+selectedKeys[0].get("themeId")+'的主题吗？', Delete); 
				}
			}]		
		},{
			dock:'bottom',
			xtype:'pagingtoolbar',	
			store:themeStore,
			displayInfo:true,
			displayMsg:'显示{0}-{1}条，共计{2}条',
			emptyMsg:"没有数据"
		},{
			dock:'bottom',
			xtype:'label',
			html:'<div style="font-size:12px;color:red">*双击可查看详细信息</div>'
		}],
		listeners:{  
			//双击事件
			itemdblclick : function( grid, record, item, index, e, eOpts){  
				var selectedKeys = grid.getSelectionModel().getSelection();
				showDetails(selectedKeys);
				
			   }  
		}
	});
	 
	function Delete(btn){
		if(btn == 'yes'){
			var selectedKeys = grid.getSelectionModel().getSelection();
			var themeId=selectedKeys[0].get("themeId");
			Ext.Ajax.request( {
	    		url : "deleteThemeAction.action", 
	    		params : {
	    			themeId : themeId
	    		}, //参数
	    		success : function(response, option) {
	    			var obj = Ext.decode(response.responseText);
	    			if(obj.success){
	    				Ext.Msg.alert("提示", "删除成功！");
	    				grid.store.reload();
	    			}else{
	    				Ext.Msg.alert("提示", "删除失败！");
	    			}
	    			
	    		},
	    		failure : function(response, option) {
	    			Ext.Msg.alert("提示", "保存失败！");
	    		}
	    		});
		}else{
			return;
		}

	}
	
	function newWindow(flag,record){
		
		var editTheme = Ext.create('Ext.Window',{
			id:'editThemeId',
			layout:'form',
			width:800,
			height:600,
			//closeAction:'hide',
			plain:true,
			modal: true,
			resizable : false,
			title:'新增指导主题',
			items:[{
				xtype:'textfield',
				fieldLabel:'编号',
				id:'themeId',
				hidden:true,
				listeners: {
		               'afterrender': function () {
		            	   //Ext.Msg.alert("ss",record);
		                   if (record!=null ){
		                	   this.setValue(record.get('themeId'));
		                   }
		               }
		           }
			},
				{
					   xtype:'textfield',  
			           fieldLabel:'标题', 
			          // width:400,
			           id:'themeName',
			           name:'themeName',  
			           allowBlank:false,  
			          // value:"主题",
			           blankText:'标题不能为空',
			           listeners: {
			               'afterrender': function () {
			            	   //Ext.Msg.alert("ss",record);
			                   if (record!=null ){
			                	   this.setValue(record.get('themeName'));
			                   }
			               }
			           }
			         
				},{
					xtype:'xhtmleditor',
	            	//width:500,
					id:'contentText',
					//name:'text',
	        		height:400,
	        		fieldLabel: '内容',
	        		allowBlank:false,  
			        blankText:'主题内容不能为空',
			      //  value:"年后",
			        uploadConfig:{
	                    url:"announcementPhotoAction.action"
	                },
	                listeners: {
			               'afterrender': function () {
			            	   //Ext.Msg.alert("ss",record);
			                   if (record!=null ){
			                	   this.setValue(record.get('contentText'));
			                   }
			               }
			           }
			},{
				//xtype:'xhtmleditor',
            	//width:500,
				id:'remark',
				//name:'text',
        		height:50,
        		fieldLabel: '备注',
        		xtype     : 'textareafield',
                grow      : true,
                anchor    : '100%',
        		//allowBlank:false,  
		        //blankText:'内容不能为空',
                listeners: {
		               'afterrender': function () {
		            	   //Ext.Msg.alert("ss",record);
		                   if (record!=null ){
		                	   this.setValue(record.get('remark'));
		                   }
		               }
		           }
        		
		},{
			xtype:'combo',
			id:'status',
	     	alias:'widget.lifechannelCombo',
	         width: 20,
	         fieldLabel: '是否启用',
	         store: isWork,
	         displayField: 'valueName',
	         valueField: 'valueId',
	         triggerAction: 'all',
	         emptyText: '请选择导师查看权限...',
	         blankText: '请选择导师查看权限',        
	         queryMode:'local',
	         listeners: {
	               'afterrender': function () {
	            	   //Ext.Msg.alert("ss",record);
	                   if (record!=null ){
	                	   this.setValue(record.get('status'));
	                   }else{
	                	   this.setValue(isWork.getAt(1).data.valueId);
	                   }
	               }
	           }
	        
		}],
			buttons : [ {
				xtype : "button",
				text : "保存",
				pressed : false,
				handler : function(){
					validatorData(flag);
					grid.store.reload();
				}
			},{
				xtype : "button",
				text : "重置",
				pressed : false,
				handler : function (){
					Ext.getCmp('themeName').setValue("");
					Ext.getCmp('contentText').setValue("");
					Ext.getCmp('remark').setValue("");
				}
			}],
			
		buttonAlign : "center",
		plain:true
			
		});
		editTheme.show();
	}
	
	function validatorData(flag){ 
		
		var themeName = Ext.getCmp('themeName').getValue();
		if(themeName == "" || themeName == null){
			Ext.Msg.alert("提示","主题名称不能为空");
			return;
		}
		var contentText = Ext.getCmp('contentText').getValue();
		if(contentText == "" || contentText == null){
			Ext.Msg.alert("提示","主题内容不能为空");
			return;
		}
		var remark = Ext.getCmp('remark').getValue();
	
		var status = Ext.getCmp('status').getValue();
		/*if(status == null || status == ''){
			Ext.Msg.alert("提示","请选择导师查看权限！");
			return;
		}*/
		//Ext.Msg.alert("提示",remark);
		var url = 'addThemeAction.action';
		var themeId = '0';
		if(!flag){
			themeId = Ext.getCmp('themeId').getValue();
			url = 'updateThemeAction.action';
		}
			
		Ext.Ajax.request({  
	        url : url,  
	        params : {  
	        	themeId:themeId,
	        	themeName : themeName,
	        	contentText:contentText,
	        	status:status,
	        	remark:remark
	        },  
	        method : 'POST',  
	        success : function(response, opts) {  
	            var success = Ext.decode(response.responseText).success;  
	            // 当后台数据同步成功时  
	            if (success) {  
	            	themeStore.load();
	            	Ext.Msg.alert("提示","主题保存成功！");
	            	Ext.getCmp('editThemeId').close();
	            } else {  
	            	Ext.Msg.alert("提示",Ext.decode(response.responseText).msg);
	            }  
	        }  
	        
	    });  
	}
	
	function showDetails(data){
		
		var tabId = "themeId" + data[0].get("themeId");
    	//var tabName = data[0].get("title").length > 15?data[0].get("title").substring(0,20):data[0].get("title");
    	/*var url = 'jsp/manager/showGuideTheme.jsp?';
    	url+="&themeName="+data[0].get("themeName");
    	url +='&contentText='+data[0].get("contentText");
    	url +="&remark="+data[0].get("remark");
    //	url.replace('"', '/'');
    	url = url.replace(/\"/g,"'");*/
    //	alert(url);
		var tab = center.queryById(tabId);
	        if (!tab) {
	        	var tab = center.add({
	        		id: tabId,
	        		iconCls: 'Bookopen',       
	        		xtype: "panel",
	        		title: "指导主题",
	        		closable: true,
	        		//maskDiabled: true,
	        		frame: true,
	        		autoScroll : true,
	        		region:center,
	        		//baseCls:'allBackImage',
	        		bodyStyle: 'background:white;',
	        		//html:'<iframe scrolling="auto" frameborder="0" width="100%" height="100%" src="'+url+'"></iframe>'
	        		html:'<div style=" border: 2px solid #379082;border-radius: 20px;width:80%;margin:auto;"><center><h3>'+data[0].get("themeName")+'</h3></center><hr style="color:#aabbcc;width:90%"><div style="width:90%;margin:auto;">'+data[0].get("contentText")+'<br><br><br><br><br><span style="font-size:12px;">备注：'+data[0].get("remark")+'</span><br><br></div></div>'
	        	});
	        	
	        }
	     //   var remark = data[0].get("remark")==''?'无':data[0].get("remark");
	       
    	center.setActiveTab(tabId);
	}
	 return grid; 
	//return ;
}